from PySide6.QtCore import QThread, Signal
import json
import requests
import time


class WaitTasks(QThread):
    trigger = Signal(list)
    is_running = True

    def __init__(self):
        super().__init__()
        self.res = requests.Session()
        self.url = "http://lease.3vppt.com/api/wyTask/"
        self.headers = {"Content-Type": "application/json"}

    def setToken(self, token):
        self.headers["Authorization"] = token

    def run(self):
        while self.is_running:
            try:
                r = self.res.post(self.url, headers=self.headers, verify=False)
                res = r.json() if r and r.status_code == 200 else None
            except BaseException as err:
                print(err)
            else:
                if res and res.get("resmsg"):
                    self.trigger.emit(res.get("resmsg"))
            time.sleep(3)
